package com.lyrcsoft.rxjava1x.operators.convert;

import lombok.extern.slf4j.Slf4j;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

/**
 * @author fujc2dev@126.com
 * @date 2024-11-20
 */
@Slf4j
public class Rx1scan {
    public static void main(String[] args) {
        log.debug("----------- rxJavaScan ---------");
        Observable.just(1, 2, 3, 4, 5)
                .scan(5,new Func2<Integer, Integer, Integer>() {
                    @Override
                    public Integer call(Integer integer, Integer integer2) {
                        return integer + integer2;
                    }
                }).subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        System.out.println(integer + " ");
                    }
                });

        log.debug("----------- rxJavaScan ---------");
        Observable.just(1, 2, 3, 4)
                .scan((sum, item) -> sum + item)
                .subscribe(System.out::println); // 输出所有中间结果

        log.debug("----------- rxJavaReduce ---------");
        Observable.just(1, 2, 3, 4)
                .reduce((sum, item) -> sum + item)
                .subscribe(System.out::println); // 只输出最终结果

    }
}
